package com.telit.firecommand.service.impl;

import com.telit.common.dto.PatrolAndCruiseCountDTO;
import com.telit.firecommand.dao.CommEventDao;
import com.telit.firecommand.dao.CommFireAlarmDao;
import com.telit.firecommand.service.FireAndEventHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther amy
 * @Describe
 */
@Service
public class FireAndEventHomeServiceImpl implements FireAndEventHomeService {

    @Autowired
    private CommFireAlarmDao commFireAlarmDao;

    @Autowired
    private CommEventDao commEventDao;

    @Override
    public Map<String, Object> selectFireAndEventCount() {
        Map<String, Object> resultMap = new HashMap<>();
        Integer patrolFireCount = 0;
        Integer cruiseFireCount = 0;
        Integer patrolEventCount = 0;
        Integer cruiseEventCount = 0;
        // 火情次数
        List<PatrolAndCruiseCountDTO> list = commFireAlarmDao.selectFireCountBySource();
        if (!CollectionUtils.isEmpty(list)) {
            for (PatrolAndCruiseCountDTO dto : list) {
                // 护林员上报火情次数
                if ("1".equals(dto.getResource())) {
                    patrolFireCount = dto.getCount();
                }

                // 巡航上报火情次数 ,无人机或者飞手渠道
                if ("2".equals(dto.getResource()) || "5".equals(dto.getResource())) {
                    cruiseFireCount = cruiseFireCount + dto.getCount();
                }
            }
        }

        // 事件上报次数
        List<PatrolAndCruiseCountDTO> eventList = commEventDao.selectCountByResource();
        if (!CollectionUtils.isEmpty(eventList)) {
            for (PatrolAndCruiseCountDTO dto : eventList) {
                // 护林员上报事件次数
                if ("1".equals(dto.getResource())) {
                    patrolEventCount = dto.getCount();
                }

                // 飞手上报时间次数
                if ("2".equals(dto.getResource())) {
                    cruiseEventCount = dto.getCount();
                }
            }
        }
        // 巡护上报火情个数
        resultMap.put("patrolFireCount", patrolFireCount);
        resultMap.put("cruiseFireCount", cruiseFireCount);
        resultMap.put("cruiseEventCount", cruiseEventCount);
        resultMap.put("patrolEventCount", patrolEventCount);

        return resultMap;
    }

    @Override
//    @Cacheable(value = "firecommand:resoure:selectFireAndEventCountByDistrictCode",depict = "首页-巡护巡航上报火情/事件总数")
    public Map<String, Object> selectFireAndEventCountByDistrictCode(String districtCode) {
        Map<String, Object> resultMap = new HashMap<>();
        Integer patrolFireCount = 0;
        Integer cruiseFireCount = 0;
        Integer patrolEventCount = 0;
        Integer cruiseEventCount = 0;
        Integer fireCount = 0;//火情总数
        Integer eventCount = 0;//事件总数

        // 火情次数
        List<PatrolAndCruiseCountDTO> list = commFireAlarmDao.selectFireCountBySourceByDistrictCode(districtCode);
        if (!CollectionUtils.isEmpty(list)) {
            for (PatrolAndCruiseCountDTO dto : list) {
                // 护林员上报火情次数
                if ("1".equals(dto.getResource())) {
                    patrolFireCount = dto.getCount();
                }

                // 巡航上报火情次数 ,无人机或者飞手渠道
                if ("2".equals(dto.getResource()) || "5".equals(dto.getResource())) {
                    cruiseFireCount = cruiseFireCount + dto.getCount();
                }

                fireCount += dto.getCount();
            }
        }

        // 事件上报次数
        List<PatrolAndCruiseCountDTO> eventList = commEventDao.selectCountByResourceByDistrictCode(districtCode);
        if (!CollectionUtils.isEmpty(eventList)) {
            for (PatrolAndCruiseCountDTO dto : eventList) {
                // 护林员上报事件次数
                if ("1".equals(dto.getResource())) {
                    patrolEventCount = dto.getCount();
                }

                // 飞手上报时间次数
                if ("2".equals(dto.getResource())) {
                    cruiseEventCount = dto.getCount();
                }

                eventCount += dto.getCount();
            }
        }
        // 巡护上报火情个数
        resultMap.put("patrolFireCount", patrolFireCount);
        resultMap.put("cruiseFireCount", cruiseFireCount);
        resultMap.put("cruiseEventCount", cruiseEventCount);
        resultMap.put("patrolEventCount", patrolEventCount);
        resultMap.put("fireCount", fireCount);
        resultMap.put("eventCount", eventCount);

        return resultMap;
    }
}
